package com.fili.gui.components;

import com.fili.core.CompileParam;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 6, 2006
 * Time: 1:17:05 PM
 * Revision: 0
 */
public class ParamGroup extends JComponent
{
    private CompileParam param;
    AbstractButton[] listData;


    public ArrayList getSelection()
    {
        ArrayList al = new ArrayList();

        for (int i = 0; i < listData.length; i++)
        {
            AbstractButton ab = listData[i];
            if (ab.isSelected())
                al.add(ab.getText());
        }

        return al;
    }

    public ParamGroup(CompileParam param)
    {
        this.param = param;

        setLayout(new BorderLayout());
        JLabel lbl = new JLabel("  " + param.getName());
        lbl.setFont(lbl.getFont().deriveFont(Font.BOLD));
        add(lbl, BorderLayout.NORTH);
        if (!param.isExclusive())
        {
            lbl.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            lbl.addMouseListener(new MouseListener()
            {
                public void mouseClicked(MouseEvent e)
                {
                    flipValues();
                }

                public void mousePressed(MouseEvent e)
                {
                }

                public void mouseReleased(MouseEvent e)
                {
                }

                public void mouseEntered(MouseEvent e)
                {
                }

                public void mouseExited(MouseEvent e)
                {
                }
            });
        }

        if (!param.isExclusive())
        {
            String[] v = param.getValues();
            listData = new AbstractButton[v.length];

            for (int i = 0; i < v.length; i++)
                listData[i] = new JCheckBox(v[i]);
        }
        else
        {
            String[] v = param.getValues();
            listData = new AbstractButton[v.length];
            ButtonGroup grp = new ButtonGroup();

            for (int i = 0; i < v.length; i++)
            {
                listData[i] = new JRadioButton(v[i]);
                if (i == 0)
                    listData[i].setSelected(true);
                grp.add(listData[i]);
            }
        }

        add(new JScrollPane(new ListPanel(listData)), BorderLayout.CENTER);
    }

    private void flipValues()
    {
        boolean sel = !listData[0].isSelected();

        for (int i = 0; i < listData.length; i++)
        {
            AbstractButton ab = listData[i];
            ab.setSelected(sel);
        }
    }

    class ListPanel extends JComponent implements Scrollable
    {
        JComponent[] list;

        public ListPanel(JComponent[] list)
        {
            this.list = list;
            setLayout(new GridLayout(list.length, 1));

            for (int i = 0; i < list.length; i++)
            {
                JComponent cmp = list[i];
                add(cmp);
            }
        }

        public Dimension getPreferredScrollableViewportSize()
        {
            return new Dimension(100, list.length * 20);
        }

        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
        {
            return 20;
        }

        public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
        {
            return 100;
        }

        public boolean getScrollableTracksViewportWidth()
        {
            return false;
        }

        public boolean getScrollableTracksViewportHeight()
        {
            return false;
        }
    }
}
